package com.zyd.shiro.controller;

import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.User;
import com.zyd.shiro.business.enums.ResponseStatus;
import com.zyd.shiro.business.service.SchoolService;
import com.zyd.shiro.business.vo.SchoolConditionVO;
import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.persistence.beans.School;
import com.zyd.shiro.util.ResultUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/school")
public class RestSchoolController {

    @Autowired
    private SchoolService schoolService;

    @RequiresPermissions("school")
    @PostMapping("/list")
    public PageResult getAll(SchoolConditionVO vo) {
        PageInfo<School> pageInfo = schoolService.findPageBreakByCondition(vo);
        return ResultUtil.tablePage(pageInfo);
    }

    @RequiresPermissions("school:add")
    @PostMapping(value = "/add")
    public ResponseVO add(School school) {
        schoolService.insert(school);
        return ResultUtil.success("成功");
    }

    @RequiresPermissions(value = {"school:batchDelete", "school:delete"}, logical = Logical.OR)
    @PostMapping(value = "/remove")
    public ResponseVO remove(Long[] ids) {
        if (null == ids) {
            return ResultUtil.error(500, "请至少选择一条记录");
        }
        for (Long id : ids) {
            schoolService.removeByPrimaryKey(id);
        }
        return ResultUtil.success("成功删除 [" + ids.length + "] 个学院");
    }

    @RequiresPermissions("school:edit")
    @PostMapping("/get/{id}")
    public ResponseVO get(@PathVariable Long id) {
        return ResultUtil.success(null, this.schoolService.getByPrimaryKey(id));
    }

    @RequiresPermissions("school:edit")
    @PostMapping("/edit")
    public ResponseVO edit(School school) {
        try {
            schoolService.updateSelective(school);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("学院修改失败！");
        }
        return ResultUtil.success(ResponseStatus.SUCCESS);
    }
}
